package com.yt.web.unit;

import java.io.Serializable;
import java.util.ArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 
 * @author Administrator
 * 
 * @param <T>
 */
public class AppJsonBase<T> implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -8637111819477625638L;
    
    private Boolean success = true;
    private T rows;
    private T result;
    private String error;
    private Integer total = 0;

    /**
     * 构造方法
     */
    public AppJsonBase() {}
    
    public AppJsonBase(int a) {
    	this.status=200;
    }
    public AppJsonBase(T rows) {
        this.rows = rows;
    }
    public AppJsonBase(String errorMessage) {
        this.success = false;
        this.error = errorMessage;
    }
    /**
     * getter、setter
     */
    public Boolean getSuccess() {
        return this.success;
    }
    public T getRows() {
        return rows == null ? (T) new ArrayList() : rows;
    }
    public void setRows(T rows) {
        this.rows = rows;
    }
    public void setTotal(Integer count) {
        this.total = count;
    }
    public Integer getTotal() {
        return this.total;
    }
	public T getResult() {
		return result;
	}
	public void setResult(T result) {
		this.result = result;
	}
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public void setSuccess(Boolean success) {
		this.success = success;
	}
	
	/**
	 * 请求状态，正常200
	 */
	private int status;
	
	public JSONObject toJson()
	{
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("status", status);
		
		JSONObject jsonData = new JSONObject();
		jsonData.put("success", success);
		
		if (null != error){
			jsonData.put("error", error);
		} else {
			jsonData.put("error", "");
		}
		
		if (null == result){
			result =(T) "";
		} else {
		
			
			result = (T) JSON.toJSONString(result,SerializerFeature.WriteMapNullValue);
		}
		
		jsonData.put("result", result);
		
		jsonResult.put("data", jsonData);
		
		return jsonResult;
	}
	
}